
/**
 *
 * @author Xinchro
 */
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class RSSFeed {

    RSSGUI gui;
    InputReader input;
    private static RSSFeed instance = null;

    public static void main(String[] args) {
        //RSSFeed reader = RSSFeed.getInstance();
        RSSFeed feed = new RSSFeed();
        //feed.writeNews("http://www.guildwars2.com/en/rss.xml");
        feed.writeGib();
    }
    
    public void writeGib(){
        for(int i=0;i<50;i++){
        gui.addText("blah");
        gui.addText("\n");}
    }

    public RSSFeed() {
        input = new InputReader(this);
        gui = new RSSGUI(this);

    }

    public static RSSFeed getInstance() {
        if (instance == null) {
            instance = new RSSFeed();
        }
        return instance;
    }

    public void writeNews(String url) {
        gui.txtArea.setText("");
        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            URL u = new URL(url);
            Document doc = builder.parse(u.openStream());

            NodeList nodes = doc.getElementsByTagName("item");
            NodeList nodes2 = doc.getElementsByTagName("entry");

            //System.out.println(nodes.getLength());    
            //System.out.println(nodes.item(0).getTextContent());
            //System.out.println(nodes.item(0).getNodeName());

            //System.out.println(nodes.getLength());
            //System.out.println(nodes2.getLength());

            for (int i = 0; i < nodes2.getLength(); i++) {

                //System.out.println(i);

                //System.out.println("\n");

                Element element = (Element) nodes2.item(i);
                //System.out.println(nodes.item(i).getAttributes().toString());

                //System.out.println(this.getElementValue(element, ""));

                gui.addText("TITLE: " + getElementValue(element, "title") + "\n");
                gui.addText("LINK: " + getElementValue(element, "link") + "\n");
                gui.addText("DATE: " + getElementValue(element, "pubDate") + "\n");
                gui.addText("AUTHOR: " + getElementValue(element, "author") + "\n");
                gui.addText("COMMENTS: " + getElementValue(element, "comments") + "\n");
                gui.addText("DESCRIPTION: " + getElementValue(element, "description") + "\n");
                gui.addText("\n");
                gui.addText("----------------------------------------------------------------" + "\n");
            }

            for (int i = 0; i < nodes.getLength(); i++) {

                //System.out.println(i);

                //System.out.println("\n");

                Element element = (Element) nodes.item(i);
                System.out.println(element);
                //System.out.println(nodes.item(i).getAttributes().toString());

                //System.out.println(this.getElementValue(element, ""));

                gui.addText("TITLE: " + getElementValue(element, "title") + "\n");
                gui.addText("LINK: " + getElementValue(element, "link") + "\n");
                gui.addText("DATE: " + getElementValue(element, "pubDate") + "\n");
                gui.addText("AUTHOR: " + getElementValue(element, "author") + "\n");
                gui.addText("COMMENTS: " + getElementValue(element, "comments") + "\n");
                gui.addText("DESCRIPTION: " + getElementValue(element, "description") + "\n");
                gui.addText("\n");
                gui.addText("----------------------------------------------------------------" + "\n");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    private String getCharacterDataFromElement(Element e) {
        try {
            Node child = e.getFirstChild();
            if (child instanceof CharacterData) {
                CharacterData cd = (CharacterData) child;
                return cd.getData();
            }
        } catch (Exception ex) {
        }
        return "";
    }

    protected float getFloat(String value) {
        if (value != null && !value.equals("")) {
            return Float.parseFloat(value);
        }
        return 0;
    }

    protected String getElementValue(Element parent, String label) {
        return getCharacterDataFromElement((Element) parent.getElementsByTagName(label).item(0));
    }
}
